if getElementData(localPlayer,"player:uid") then return end

loadstring(exports.dgs:dgsImportFunction())()

cmtaGP = false
local sw,sh = guiGetScreenSize()
local px,py = sw/1366,sh/768
font = dgsCreateFont( ":cmta-pobieraniezasobow/OSB.ttf", 12*px )
font2 = dgsCreateFont( ":cmta-pobieraniezasobow/OSB.ttf", 11*px )
background = dgsCreateImage(0*px,0*py,1366*px,768*py,"background.jpg",false,false,tocolor(255,255,255,255))
-- Glowny panel --
window = dgsCreateWindow(300*px,100*py,766*px,568*py,"panel logowania",false,false,false,false,tocolor(210,105,30),false,false,false,true)
button = dgsCreateButton( 30*px, 30*py, 166*px, 40*py, "Informacje", false,window,false,false,false,false,false,false,false,tocolor(90,90,90),tocolor(255,0,0,90))
button2 = dgsCreateButton( 210*px, 30*py, 166*px, 40*py, "Regulamin", false,window,false,false,false,false,false,false,false,tocolor(90,90,90),tocolor(255,0,0,90))
button3 = dgsCreateButton( 390*px, 30*py, 166*px, 40*py, "Zaloguj się", false,window,false,false,false,false,false,false,false,tocolor(90,90,90),tocolor(255,0,0,90))
button4 = dgsCreateButton( 570*px, 30*py, 166*px, 40*py, "Zarejestruj się", false,window,false,false,false,false,false,false,false,tocolor(90,90,90),tocolor(255,0,0,90)) 


memo = dgsCreateMemo(50*px,100*py,666*px,368*py,"",false,window)
dgsMemoInsertText(memo,1,1,"Cześć!\n\nJeśli jesteś tutaj po raz pierwszy zapoznaj się z informacjami i regulaminem")
dgsSetFont( memo, font )
dgsSetEnabled(memo,false)
dgsSetVisible ( memo,true )
dgsSetAlpha(memo,255)

memo2 = dgsCreateMemo(50*px,100*py,666*px,368*py,"",false,window)
dgsMemoInsertText(memo2,1,1,"Regulamin serwera\n1. Edysia jest najważniejsza.")
dgsSetFont( memo2, font )
dgsSetEnabled(memo2,false)
dgsSetVisible ( memo2,false )
dgsSetAlpha(memo2,0)
dgsWindowSetMovable (window, false )
dgsWindowSetSizable (window, false )



-- Panel logowania --
window2 = dgsCreateWindow(300*px,200*py,766*px,468*py,"",false,false,false,false,false,false,false,false,true)
edit1 = dgsCreateEdit( 220*px, 120*py, 326*px, 30*py, "Login:", false,window2 )
edit2 = dgsCreateEdit( 220*px, 180*py, 326*px, 30*py, "Hasło", false,window2 )
dgsSetProperty(edit1,"alignment",{"center","center"})
dgsSetProperty(edit2,"alignment",{"center","center"})
button5 = dgsCreateButton( 260*px, 280*py, 246*px, 40*py, "Zaloguj", false,window2,false,false,false,false,false,false,false,tocolor(90,90,90),tocolor(255,0,0,90))
dgsWindowSetMovable (window2, false )
dgsWindowSetSizable (window2, false )
dgsSetVisible ( window2,false )
dgsSetAlpha(window2,0)
text = dgsCreateLabel(0*px,390*py,766*px,50*py,"#ff0000Pamiętaj, że używanie niedozwolonego oprogramowania wiąże się ze stałą blokadą konta.",false,window2)
dgsSetFont( text, font2 )
dgsSetProperty(text,"colorcoded",true)
dgsSetProperty(text,"alignment",{"center","center"})
dgsEditSetMasked ( edit2, true )
checklog = dgsCreateCheckBox(600*px, 240*py, 326*px, 30*py,"Zapamiętaj dane",false,false,window2)


text2 = dgsCreateLabel(0*px,50*py,766*px,50*py,"",false,window2)
dgsSetFont( text2, font2 )
dgsSetProperty(text2,"colorcoded",true)
dgsSetProperty(text2,"alignment",{"center","center"})


-- Panel rejestracji --
window3 = dgsCreateWindow(300*px,200*py,766*px,468*py,"",false,false,false,false,false,false,false,false,true)
edit3 = dgsCreateEdit( 220*px, 50*py, 326*px, 30*py, "Login:", false,window3 )
edit4 = dgsCreateEdit( 220*px, 100*py, 326*px, 30*py, "Hasło:", false,window3 )
edit5 = dgsCreateEdit( 220*px, 150*py, 326*px, 30*py, "Powtórz hasło:", false,window3 )
edit6 = dgsCreateEdit( 220*px, 200*py, 326*px, 30*py, "E-mail:", false,window3 )
edit7 = dgsCreateEdit( 220*px, 250*py, 326*px, 30*py, "PIN: (1-9)", false,window3 )
dgsEditSetMaxLength ( edit7, 4 )
dgsSetProperty(edit3,"alignment",{"center","center"})
dgsSetProperty(edit4,"alignment",{"center","center"})
dgsSetProperty(edit5,"alignment",{"center","center"})
dgsSetProperty(edit6,"alignment",{"center","center"})
dgsSetProperty(edit7,"alignment",{"center","center"})
dgsWindowSetMovable (window3, false )
dgsWindowSetSizable (window3, false )
dgsSetVisible ( window3,false )
dgsSetAlpha(window3,0)
button6 = dgsCreateButton( 110*px, 350*py, 246*px, 40*py, "Stwórz konto", false,window3,false,false,false,false,false,false,false,tocolor(90,90,90),tocolor(255,0,0,90))
button7 = dgsCreateButton( 410*px, 350*py, 246*px, 40*py, "Wyczyść", false,window3,false,false,false,false,false,false,false,tocolor(90,90,90),tocolor(255,0,0,90))
check = dgsCreateCheckBox(550*px, 300*py, 326*px, 30*py,"Akceptuje regulamin serwera",false,false,window3)

text3 = dgsCreateLabel(0*px,0*py,766*px,50*py,"",false,window3)
dgsSetFont( text3, font2 )
dgsSetProperty(text3,"colorcoded",true)
dgsSetProperty(text3,"alignment",{"center","center"})


function onclick ( btn, state )
	if btn == "left" and state == "down" and cmtaGP == true then
		dgsMoveToBack ( window )
		dgsMoveToBack ( background )
		if source == button then
			if dgsIsAlphaing(memo) or dgsIsAlphaing(memo2) or dgsIsAlphaing(window2) or dgsIsAlphaing(window3) then return end
			dgsSetEnabled(window2,false)
			dgsSetVisible ( memo,true )
			dgsAlphaTo(memo2,0,false,"OutQuad",2000)
			dgsAlphaTo(window2,0,false,"OutQuad",2000)
			dgsAlphaTo(window3,0,false,"OutQuad",2000)
            setTimer(function()
			dgsAlphaTo(memo,255,false,"OutQuad",2000)
			end,1800,1)
		end
		if source == button2 then
			if dgsIsAlphaing(memo) or dgsIsAlphaing(memo2) or dgsIsAlphaing(window2) or dgsIsAlphaing(window3) then return end
			dgsSetEnabled(window2,false)
			dgsSetVisible ( memo2,true )
			dgsAlphaTo(memo,0,false,"OutQuad",2000)
			dgsAlphaTo(window2,0,false,"OutQuad",2000)
			dgsAlphaTo(window3,0,false,"OutQuad",2000)
            setTimer(function()
			dgsAlphaTo(memo2,255,false,"OutQuad",2000)
			end,1800,1)
		end
		if source == button3 then
			if dgsIsAlphaing(memo) or dgsIsAlphaing(memo2) or dgsIsAlphaing(window2) or dgsIsAlphaing(window3) then return end
			dgsSetVisible ( window2,true )
			dgsSetEnabled(window2,true)
			dgsBringToFront ( window2 )
			load_rememberMe()
			dgsAlphaTo(memo,0,false,"OutQuad",2000)
			dgsAlphaTo(memo2,0,false,"OutQuad",2000)
			dgsAlphaTo(window3,0,false,"OutQuad",2000)
			setTimer(function()
			dgsAlphaTo(window2,5,false,"OutQuad",2000)
			end,1800,1)
		end
		if source == button4 then
			if dgsIsAlphaing(memo) or dgsIsAlphaing(memo2) or dgsIsAlphaing(window2) or dgsIsAlphaing(window3) then return end
			dgsSetVisible ( window3,true )
			dgsSetEnabled(window3,true)
			dgsBringToFront ( window3 )
			dgsAlphaTo(memo,0,false,"OutQuad",2000)
			dgsAlphaTo(memo2,0,false,"OutQuad",2000)
			dgsAlphaTo(window2,0,false,"OutQuad",2000)
			setTimer(function()
			dgsAlphaTo(window3,5,false,"OutQuad",2000)
			end,1800,1)
		end

		if source == button5 then
		local login = dgsGetText ( edit1 )
		local password = dgsGetText ( edit2 )
		if string.len(login) < 3 then exports["PSSM-notyfikacje"]:pssmnoti2("#ff0000Na stanowisku nie znajduje się żaden pojazd.",localPlayer) return end 
		triggerServerEvent("cmta:login", resourceRoot, login, password)
		end
		if source == button6 then
			local login = dgsGetText ( edit3 )
			local password = dgsGetText ( edit4 )
			local rpassword = dgsGetText ( edit5 )
			local email = dgsGetText ( edit6 )
			local pin = dgsGetText ( edit7 )

			if string.len(login) < 3 or string.len(login) > 24 then dgsSetText(text3, "#ff0000Twój login musi mieć od 3-24 znaków") return end
			if string.len(password) < 3 or string.len(password) > 24 then dgsSetText(text3, "#ff0000Twoje hasło musi mieć od 3-24 znaków") return end
			if password ~= rpassword then dgsSetText(text3, "#ff0000Podane hasła różnią się od siebie") return end
			if not tonumber(pin) or string.find(pin, "0") or pin:len() ~= 4 then dgsSetText(text3, "#ff0000PIN musi składać się z 4 cyfr (1-9)") return end
			if not string.find(email, "@") then dgsSetText(text3, "#ff0000Twój e-mail jest nieprawidłowy. Podaj prawdziwy, aby móc odzyskać hasło.") return end
            if not dgsCheckBoxGetSelected(check) then dgsSetText(text3, "#ff0000Musisz zaakceptować regulamin") return end
			triggerServerEvent("cmta:register", resourceRoot, login, password,email,pin)
		end
		if source == button7 then
        dgsSetText(edit3,"Login:")
        dgsSetText(edit4,"Hasło:")
        dgsSetText(edit5,"Powtórz hasło:")
        dgsSetText(edit6,"E-mail:")
        dgsSetText(edit7,"PIN:")
		dgsCheckBoxSetSelected(check,false)
		end
		if source == spawnsf then
			cmtaGP = false
			dgsAlphaTo(background,0,false,"OutQuad",2000)
			dgsAlphaTo(rect1,0,false,"OutQuad",2000)
			dgsAlphaTo(rect2,0,false,"OutQuad",2000)
			dgsAlphaTo(rect3,0,false,"OutQuad",2000)
			setElementPosition(localPlayer,-2279, 157.3, 35.1)
			setTimer(function()
			dgsSetVisible ( background,false )
			dgsSetVisible ( rect1,false )
			dgsSetVisible ( rect2,false )
			dgsSetVisible ( rect3,false )
			showCursor(false)
			showChat(true)
			triggerServerEvent("cmta:checkbonus",resourceRoot)
			destroy()
			end,1800,1)
		end
	end
end

addEvent("cmta:succeslogin", true)
addEventHandler("cmta:succeslogin", resourceRoot, function()
	dgsAlphaTo(window,0,false,"OutQuad",2000)
	dgsAlphaTo(window2,0,false,"OutQuad",2000)
	 rndRect = dgsCreateRoundRect(30,false,tocolor(0,0,0,150))  
	 rect1 = dgsCreateImage(100*px,150*py,366*px,100*py,rndRect,false)
	 rect2 = dgsCreateImage(500*px,150*py,366*px,100*py,rndRect,false)
	 rect3 = dgsCreateImage(900*px,150*py,366*px,100*py,rndRect,false)
	 dgsSetAlpha(rect1,0)
	 dgsSetAlpha(rect2,0)
	 dgsSetAlpha(rect3,0)
	 dgsAlphaTo(rect1,255,false,"OutQuad",2000)
	 dgsAlphaTo(rect2,255,false,"OutQuad",2000)
	 dgsAlphaTo(rect3,255,false,"OutQuad",2000)
	spawnsf = dgsCreateLabel(0*px,0*py,366*px,100*py,"San Fierro",false,rect1)
	spawnls = dgsCreateLabel(0*px,0*py,366*px,100*py,"Los Santos",false,rect2)
	spawnlv = dgsCreateLabel(0*px,0*py,366*px,100*py,"Las Venturas",false,rect3)
	dgsSetProperty(spawnsf,"alignment",{"center","center"})
	dgsSetProperty(spawnls,"alignment",{"center","center"})
	dgsSetProperty(spawnlv,"alignment",{"center","center"})
	dgsSetFont( spawnsf, font)
	dgsSetFont( spawnls, font)
	dgsSetFont( spawnlv, font)
	save_rememberMe()
end)

addEvent("cmta:text", true)
addEventHandler("cmta:text", resourceRoot, function(message)
	dgsSetText(text2, message)
end)

addEvent("cmta:regtext", true)
addEventHandler("cmta:regtext", resourceRoot, function(message)
	dgsSetText(text3, message)
end)


addEventHandler("onClientResourceStart", resourceRoot, function()
	if cmtaGP == false then
	showCursor(true)
	dgsSetVisible ( window,true )
	cmtaGP = true
	addEventHandler ( "onDgsMouseClick", getRootElement(), onclick)
	music=playSound("music.mp3",true)
	setSoundVolume(music, 0.3)
    end
end)

function load_rememberMe()
    local xml=xmlLoadFile("datasave.xml")
    if not xml then return end
        
    local xmlNode=xmlFindChild(xml, "login", 0)
    local login=xmlNodeGetValue(xmlNode)
    if login then
		dgsSetText(edit1, tostring(login))
    end
    local xmlNode=xmlFindChild(xml, "pass", 0)
    local passwd=xmlNodeGetValue(xmlNode)
    if passwd then 
		dgsSetText(edit2, teaDecode(tostring(passwd),">,qx*+=zb9i6)Z2"))
	end
	local xmlNode=xmlFindChild(xml, "activebutton", 0)
    local button=xmlNodeGetValue(xmlNode)
    if button then
		dgsCheckBoxSetSelected(checklog,true)
	end
end

function save_rememberMe()
    if dgsCheckBoxGetSelected(checklog) then
        local xml=xmlCreateFile("datasave.xml", "auth")
        if not xml then return end
        xmlNodeSetValue(xmlCreateChild(xml, "login"), dgsGetText (edit1))
		xmlNodeSetValue(xmlCreateChild(xml, "pass"), teaEncode(dgsGetText (edit2),">,qx*+=zb9i6)Z2"))
		xmlNodeSetValue(xmlCreateChild(xml, "activebutton"), "tak")
        xmlSaveFile(xml)
	else
		fileDelete("datasave.xml")
	end
end

function destroy()
	destroyElement(background)
	destroyElement(window)
	destroyElement(window2)
	destroyElement(window3)
	destroyElement(rect1)
	destroyElement(rect2)
	destroyElement(rect3)
	destroyElement(rndRect)
	removeEventHandler ( "onDgsMouseClick", getRootElement(), onclick)
	stopSound(music)
	exports["cmta-hud"]:createhud(localPlayer) 
end
